package ${t.packagePath}.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Date;
import ${t.packagePath}.service.${t.apiLogTableName}Service;
import ${t.packagePath}.utils.AjaxJson;
import ${t.packagePath}.config.StpSpUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import java.lang.reflect.Method;
import cn.hutool.core.util.ReflectUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.mkNameBig}Service;
import ${t.packagePath}.model.${t.apiLogTableName};
<#if cfg.saTokenAuthWay == 1 >import cn.dev33.satoken.stp.StpUtil;
<#else>import cn.dev33.satoken.annotation.SaCheckPermission;
</#if>


/**
 * Controller: API请求日志
 * @author ${cfg.author} 
 */
@RestController
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ${t.apiLogTableName}Controller {

	${t.apiLogTableName}Service ${t.apiLogTableName?uncap_first}Service;
	BeanSearcher searcher;


	@ApiOperation("删")
	@${cfg.apiMappingWayString}("/SgApilog/delete")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.apiLogTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson delete(${t.primaryKey.fieldType} id){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.modelName}.PERMISSION_CODE);${cfg.line}</#if><#rt>

		boolean b = ${t.apiLogTableName?uncap_first}Service.removeById(id);
		return AjaxJson.getByBoolean(b);
	}

	/**
     * 删 - 根据id列表
     */
    @RequestMapping("/SgApilog/deleteByIds")
    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.apiLogTableName}.PERMISSION_CODE)
    public AjaxJson deleteByIds(HttpServletRequest request) {
        Map<String, Object> flat = MapUtils.flat(request.getParameterMap());
        Object object = flat.get("ids");
        if (object == null) {
            return AjaxJson.getSuccess();
        }
        List<Long> ids = Arrays.stream(object.toString().split(",")).map(Long::parseLong).collect(Collectors.toList());
		boolean b = ${t.apiLogTableName?uncap_first}Service.removeByIds(ids);
		return AjaxJson.getByBoolean(b);
    }

    /**
    * 删 - 根据日期范围
    */
    @RequestMapping("/SgApilog/deleteByStartEnd")
    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.apiLogTableName}.PERMISSION_CODE)
    public AjaxJson deleteByStartEnd(Date startTime, Date endTime) {
    	LambdaQueryWrapper<${t.apiLogTableName}> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(${t.apiLogTableName}::getStartTime, startTime, endTime);
        boolean remove = ${t.apiLogTableName?uncap_first}Service.remove(wrapper);
        return AjaxJson.getByBoolean(remove);
        }


	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@${cfg.apiMappingWayString}("/SgApilog/getList")
<#if cfg.saTokenAuthWay == 2 >   @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.apiLogTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson getList(HttpServletRequest request) {
		SearchResult<${t.apiLogTableName}> search = searcher.search(${t.apiLogTableName}.class, MapUtils.flat(request.getParameterMap()));
    	return AjaxJson.getPageData(search.getTotalCount().longValue(), search.getDataList());
	}
    /**
    * 统计
    */
    @RequestMapping("/SgApilog/staBy")
    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.apiLogTableName}.PERMISSION_CODE)
    public AjaxJson staBy(HttpServletRequest request) {
		Map<String, Object> flat = MapUtils.flat(request.getParameterMap());
		LambdaQueryChainWrapper<${t.apiLogTableName}> wrapper = ${t.apiLogTableName?uncap_first}Service.lambdaQuery();
			//        wrapper.select("sum(cost_time) as cost_time_count");
			return AjaxJson.getSuccessData(null);
	}
}
